package ru.spb.osll.myfit.server.chart;

import ru.spb.osll.myfit.util.FileHelper;

import java.io.*;

/**
 * User: Artem Bukhonov
 * Date: 22.04.12
 * Time: 17:09
 */
public class XMLChartWriter implements ChartWriter {
  private static final String FILE_EXT = "chart";

  private String m_outputDirectory;
  private final ChartXMLSerializer m_serializer = new ChartXMLSerializer();

  public XMLChartWriter(String outputDirectory) {
    m_outputDirectory = outputDirectory;
  }

  @Override
  public void write(ChartRenderData data, ChartSaveInfo info) throws IOException {
    String dataFilename = info.getIndexInTest() + FileHelper.SEPARATOR + info.getChartName() + "." + FILE_EXT;
    OutputStream dataStream = new FileOutputStream(new File(m_outputDirectory, dataFilename));
    PrintWriter writer = new PrintWriter(dataStream);
    String textData = m_serializer.toXML(data);
    writer.write(textData);
    writer.flush();
    writer.close();
  }
}
